﻿using DNFImagePacks2Manager.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

namespace DNFImagePacks2Manager.Helpers
{
    public class BKPathHelper
    {
        private static string version;
        private static ReadOnlyCollection<BKPathCategory> categoryList;
        private static ReadOnlyDictionary<string, string> categoryDirectory;

        public static ReadOnlyCollection<BKPathCategory> CategoryList => categoryList;
        public static ReadOnlyDictionary<string, string> CategoryDirectory => categoryDirectory;

        public static readonly string BKMapPath = Path.Combine(AppContext.BaseDirectory, "JsonData", "BKMap.json");

        static BKPathHelper()
        {
            version = "0.0.0";
            categoryList = new ReadOnlyCollection<BKPathCategory>([]);
            categoryDirectory = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>());
            ReloadMap();
        }

        public static void ReloadMap()
        {
            try
            {
                var tempCategoryList = InitialBuffPath();
                categoryList = new ReadOnlyCollection<BKPathCategory>(tempCategoryList);
                var tempCategoryDirectory = InitialDictionary();
                categoryDirectory = new ReadOnlyDictionary<string, string>(tempCategoryDirectory);
            }
            catch (Exception)
            {
                version = "0.0.0";
                categoryList = new ReadOnlyCollection<BKPathCategory>([]);
                categoryDirectory = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>());
            }
        }

        private static List<BKPathCategory> InitialBuffPath()
        {
            List<BKPathCategory> tempCategoryList = [];
            try
            {
                string jsonText = File.ReadAllText(BKMapPath);
                
                JObject categoryObj = JObject.Parse(jsonText);

                foreach (var categoryNode in categoryObj)
                {
                    string categoryName = categoryNode.Key;
                    JToken? categoryValue = categoryNode.Value;

                    if (categoryValue == null) continue;

                    if (categoryValue.Type == JTokenType.Object)
                    {

                        JObject subObj = (JObject)categoryValue;

                        List<BKPathCategorySub> subList = [];

                        foreach (var subNode in subObj)
                        {
                            string subName = subNode.Key;
                            JToken? subValue = subNode.Value;

                            if (subValue == null) continue;

                            if (subValue.Type == JTokenType.Object)
                            {

                                JObject pathObj = (JObject)subValue;

                                List<BKPathItem> pathList = [];

                                foreach (var pathNode in pathObj)
                                {
                                    string name = pathNode.Key;
                                    JToken? value = pathNode.Value;

                                    if (value == null) continue;

                                    string? path = value.ToString() ?? null;
                                    if (path != null)
                                    {
                                        BKPathItem item = new(name, path);
                                        pathList.Add(item);
                                    }
                                }

                                if(pathList.Count > 0)
                                {
                                    BKPathCategorySub subCategory = new(subName ,pathList);
                                    subList.Add(subCategory);
                                }
                            }
                        }

                        if (subList.Count > 0)
                        {
                            BKPathCategory subCategory = new(categoryName, subList);
                            tempCategoryList.Add(subCategory);
                        }
                    }
                    else if (categoryValue.Type == JTokenType.String)
                    {
                        if(string.Equals("版本", categoryName))
                        {
                            version = categoryValue.ToString();
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
            return tempCategoryList;
        }

        private static Dictionary<string, string> InitialDictionary()
        {
            Dictionary<string, string> tempCategoryDirectory = [];

            foreach (BKPathCategory category in CategoryList)
            {
                string categoryName = category.name;
                foreach (BKPathCategorySub subCategory in category.list)
                {
                    string subCategoryName = subCategory.name;
                    foreach (BKPathItem item in subCategory.list)
                    {
                        string key = item.path;
                        string value = $"{categoryName} - {subCategoryName} - {item.name}";
                        tempCategoryDirectory.Add(key, value);
                    }
                }
            }
            return tempCategoryDirectory;
        }
    }
}
